<?php
if (! defined ( 'BASEPATH' ))
    exit ( 'No direct script access allowed' );

class pasien_model extends Model {

    protected $_table = 'Pasien';
    protected $_prefix = 'pasien';
    
    public function __construct() {
        // Call the Model constructor
        parent::__construct ();
    }

    public function add($data) {
        if (! $data || ! is_array ( $data ))
            return false;
        return $this->db->insert ( $this->_table, $data ) && ($this->db->affected_rows () > 0) ? true : false;
    
    }
    public function update($id, $data) {
        if (! $id || ! $this->get ( $id ) || ! $data)
            return array ();
        $this->db->where ( $this->_prefix . 'Id', $id );
        $this->db->limit ( 1 );
        return $this->db->update ( $this->_table, $data ) && ($this->db->affected_rows () > 0) ? true : false;
    }
	public function delete($id) {
        if (! $id || ! $this->get ( $id ))
            return array ();
        $this->db->where ( $this->_prefix . 'Id', $id );
        $this->db->limit ( 1 );
        return $this->db->delete ( $this->_table ) && ($this->db->affected_rows () > 0) ? true : false;
    }
    
    public function get($id) {
        if (! $id) {
            return array ();
        }
        $query = $this->db->get_where ( $this->_table, array ($this->_prefix . 'Id' => $id ) );
        return $query->row_array ();
    }
    
    public function getList($limit = 100, $offset = 0, $where=array()) {
    	$this->db->select ( '*' );
        $this->db->from ( $this->_table );
        
        if( isset($where['pasienNama']) )
        	$this->db->like ( 'pasienNama', $where['pasienNama'], 'both' );
        
        $this->db->order_by ( 'pasienId', 'desc' );
        $this->db->limit ( $limit, $offset );
        $query = $this->db->get ();
        return $query->result_array ();
    }
    public function getCount($where=array()) {
        $this->db->select ( 'count(' . $this->_prefix . 'Id) as count' );
        $this->db->from ( $this->_table );
        
        if( isset($where['pasienNama']) )
        	$this->db->like ( 'pasienNama', $where['pasienNama'], 'both' );
        
        $query = $this->db->get ();
        if ($query && ($row = $query->row_array ())) {
            return $row ['count'];
        }
        return 0;
    }
    
	public function getListPasien($limit = 100, $offset = 0, $where=array()) {
        $this->db->select ( '*' );
    	$this->db->from ( $this->_table );
        
        if( isset($where['pasienBaru']) )
        	$this->db->where ( 'pasienJmlRJ <= 1' );
        if( isset($where['pasienMember']) )
        	$this->db->where ( 'pasienJmlRJ > 1' );
        	
        if( !empty($where['frdate']) && empty($where['todate']) )
        	$this->db->where ( 'DATE(pasienLastRJ)', $where['frdate'] );	
        elseif( !empty($where['frdate']) && !empty($where['todate']) )
        	$this->db->where ( 'DATE(pasienLastRJ) BETWEEN "'.$where['frdate'].'" AND "'.$where['todate'].'" ' );	
        
        $this->db->order_by ( 'pasienId', 'desc' );
        $this->db->limit ( $limit, $offset );
        $query = $this->db->get (); 
        return $query->result_array ();
    }
	public function getCountPasien( $where=array() ) { 
        $this->db->select ( 'count(' . $this->_prefix . 'Id) as count' );
        
        if( isset($where['pasienBaru']) )
        	$this->db->where ( 'pasienJmlRJ <= 1' );
        if( isset($where['pasienMember']) )
        	$this->db->where ( 'pasienJmlRJ > 1' );
        	
        if( !empty($where['frdate']) && empty($where['todate']) )
        	$this->db->where ( 'DATE(pasienLastRJ)', $where['frdate'] );	
        elseif( !empty($where['frdate']) && !empty($where['todate']) )
        	$this->db->where ( 'DATE(pasienLastRJ) BETWEEN "'.$where['frdate'].'" AND "'.$where['todate'].'" ' );	
        
        $this->db->from ( $this->_table );
        $query = $this->db->get ();
        if ($query && ($row = $query->row_array ())) {
            return $row ['count'];
        }
        return 0;
    }
    
	public function getJson($where=array()) {
		$this->db->select ( '*' );
        $this->db->from ( $this->_table );
        
        if( isset($where['field']) && isset($where['value']) )
        	$this->db->like ( $where['field'], $where['value'], 'after');
        	
        $query = $this->db->get (); 
        return $query->result_array ();
    }
}